<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/1 0001
 * Time: 下午 3:26
 */

namespace App\Http\Controllers\Admin;


use App\Role;
use App\User;
use Illuminate\Routing\Route;

class IndexController extends BaseController
{
    public function index()
    {
        //Article::created([]);
        /*$owner = new Role();
        $owner->name = 'owner';
        $owner->display_name = 'Project Owner';
        $owner->description = 'User is the owner of a given project';
        $owner->save();

        $admin = new Role();
        $admin->name = 'admin';
        $admin->display_name = 'User Administrator';
        $admin->description = 'User is allowed to manage and edit other users';
        $admin->save();
        $user = User::where('username', '=', 'admin')->first();

        //调用EntrustUserTrait提供的attachRole方法
        $user->attachRole($admin); // 参数可以是Role对象，数组或id

        // 或者也可以使用Eloquent原生的方法
        //$user->roles()->attach($admin->id); //只需传递id即可
        $createPost = new Permission();
        $createPost->name = 'create-post';
        $createPost->display_name = 'Create Posts';
        $createPost->description = 'create new blog posts';
        $createPost->save();

        $editUser = new Permission();
        $editUser->name = 'edit-user';
        $editUser->display_name = 'Edit Users';
        $editUser->description = 'edit existing users';
        $editUser->save();

        $owner->attachPermission($createPost);
        //等价于 $owner->perms()->sync(array($createPost->id));

        $admin->attachPermissions(array($createPost, $editUser));
        //等价于 $admin->perms()->sync(array($createPost->id, $editUser->id));
        var_dump($user->hasRole('owner'));
        var_dump($user->hasRole('admin'));
        var_dump($user->can('edit-user'));
        var_dump($user->can('create-post'));*/
//        $admin = new Role();
//        $admin->name = 'admin';
//        $admin->display_name = '超级管理员';
//        $admin->description = '能操作所有权限';
//        $admin->save();
//        $user = User::where('username', '=', 'admin')->first();
//        $user->attachRole($admin);
//        $admin->perms()->sync(array(1,2,7));
//        service('Route')->getAdminRoutes();
        $list = service('Admin')->permissionTree();
        return view('admin.index', ['routes' => $list['data']]);
    }

    /**
     * @description:后台首页
     * @author: hkw <hkw925@qq.com>
     */
    public function info()
    {
        //var_dump(request()->path());exit;
        return view('admin.index.info');
    }
}